#include "ClientSock.h"

CClientSock::CClientSock(tcp::endpoint &endpoint)
{

    io_service_ = new io_service();
    socket_ = new tcp::socket(*io_service_);
    boost::system::error_code ec;
    std::cout << __LINE__ << __FILE__ << std::endl;
    std::cout << endpoint.address() << " " << endpoint.port() << std::endl;
    socket_->connect(endpoint, ec);
    std::cout << __LINE__ << __FILE__ << std::endl;
    if (ec)
    {
        std::cerr << "Error: " << ec << std::endl;
        throw ec;
    }
    else
    {
        std::cout << "connected is oK!" << std::endl;
    }
}

CClientSock::~CClientSock(void)
{

    delete io_service_;
    delete socket_;
}

//
unsigned int CClientSock::send(uint32_t *pBuff, unsigned int len)
{
    boost::asio::const_buffers_1 request(pBuff, len);
    return socket_->send(request);
}

//
unsigned int CClientSock::recv(char *pBuff, int len)
{

    int retRead = socket_->read_some(boost::asio::buffer(pBuff, len));
    return retRead;
}

//
void CClientSock::close()
{

    socket_->close();
}
